<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Product extends CI_Controller {

    function __construct(){
        parent::__construct();
        $this->load->model('product/product_model');
    }

    function index(){
        $category_data = $this->product_model->get_category();
        $category[0] = '';
        foreach (@$category_data as $key => $value){
            $category[$value['category_id']] = $value['title'];
        }

        /*$manufacturer_data = $this->product_model->get_manufacturer();
        foreach($manufacturer_data as $key => $value){
            $manufacturer[$value['manufacture_id']] = $value['title'];
        }*/

        $data['category'] = @$category;
        //$data['manufacturer'] = @$manufacturer;
        $data['result'] = $this->product_model->get_many_by(array('delete_flag' => UNDELETE_FLAG, 'draft' => DRAFT_NO));
        $data['tpl'] = 'admin/product/index';
        $this->load->view('layout/master', $data);
    }

    function add(){
        $this->_update();
    }

    function edit($id){
        $this->_update($id);
    }

    function _update($product_id = 0){

		$product_id = intval($product_id);
        $info = array();

        if ($_POST){
            $product_id  = $this->input->post('product_id');
            $openbeta = explode('/', trim($this->input->post('openbeta')));
            $info = array(
                'product_id'           => $product_id,
                'title'                => $this->input->post('title'),
                'position'             => intval($this->input->post('position')),
                'openbeta'             => $openbeta[2].'-'.$openbeta[1].'-'.$openbeta[0],
                'webgame'              => intval($this->input->post('webgame')),
                'weblink'              => trim($this->input->post('weblink')),
                'update_date'          => date('Y-m-d H:i:s', time()),
                'draft'                => DRAFT_NO,
                'active_flag'          => ACTIVE_FLAG
            );

            if ($this->product_model->update($product_id, $info)) {
                $data['msg'] = 'Sản phẩm đã được '. ($product_id != 0 ? 'lưu' : 'thêm') .' thành công';
            }
        }

        /*Check draft*/
        if ($product_id == 0){
            $check_draft = $this->product_model->check_draft();
            if (is_bool($check_draft)){
                $product_id = $this->product_model->insert(array('draft' => 'yes', 'create_date' => date('Y-m-d H:i:s'), 'user_id' => $_SESSION['admin']['id']));
            } else {
                $data['data'] = $check_draft;
                $product_id   = $check_draft['product_id'];
            }
        }

        /*Info*/
        if (!isset($data['data'])){
            $data['data'] = $this->product_model->as_array()->get_by(array('product_id' => $product_id));
        }

        /*Product's image*/
        $data['images'] = $this->product_model->get_images($product_id);

        /*Category*/
        //$category = $this->product_model->get_category();
        //$data['category'] = @$category;

        /*Manufacturer*/
        //$manufacturer_data = $this->product_model->get_manufacturer();
        //foreach($manufacturer_data as $key => $value){
        //    $manufacturer[$value['manufacture_id']] = $value['title'];
        //}
        //$data['manufacturer'] = @$manufacturer;

        $data['tpl'] = 'admin/product/edit';
        $this->load->view('layout/master', $data);
    }
}